//
//  YZMapManager.m
//  VET
//
//  Created by YZ on 2018/7/12.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZMapManager.h"
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>

#define kApiKey @"f9105dc1048068592fa0be2c80c76c3e"//@"7f722042917e352375ee1d12beaf3041"

@interface YZMapManager()<AMapSearchDelegate,AMapLocationManagerDelegate>

@property (nonatomic, strong) AMapLocationManager *locationManager;

@property (nonatomic, strong) AMapSearchAPI *searchManager;

@property (nonatomic, strong) YZMapResultBlock resultBlock;

@property (nonatomic, strong) CLLocation *currentLocation;

@end

@implementation YZMapManager

+ (void)startServices
{
    [AMapServices sharedServices].apiKey = kApiKey;
}

- (CLLocation *)getCurrentLocation
{
    return self.currentLocation;
}

- (void)startLocating
{
    self.locationManager.delegate = self;
    self.locationManager.distanceFilter = 200;
    [self.locationManager startUpdatingLocation];
}

-(void)stopLocating
{
    [self.locationManager stopUpdatingLocation];
}

- (void)startSearchPOSWithCoor:(CLLocationCoordinate2D)coor  result:(YZMapResultBlock)resultBlock
{
    self.resultBlock = resultBlock;
    [self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    //   定位超时时间，最低2s，此处设置为2s
    self.locationManager.locationTimeout =2;
    //   逆地理请求超时时间，最低2s，此处设置为2s
    self.locationManager.reGeocodeTimeout = 2;
    
//    [self.locationManager requestLocationWithReGeocode:YES completionBlock:^(CLLocation *location, AMapLocationReGeocode *regeocode, NSError *error) {
//        if (error)
//        {
//            NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
//            if (self.resultBlock) {
//                self.resultBlock(NO, nil);
//            }
//            if (error.code == AMapLocationErrorLocateFailed) return;
//        }
//
//        NSLog(@"location:%@", location);
//        if (regeocode)
//        {
//            NSLog(@"reGeocode:%@", regeocode);
//        }
    
        AMapPOIAroundSearchRequest *req = [[AMapPOIAroundSearchRequest alloc] init];
        req.location = [AMapGeoPoint locationWithLatitude:coor.latitude longitude:coor.longitude];
        req.types = @"商务住宅";
        req.sortrule = 0;
        req.requireExtension = YES;
        [self.searchManager AMapPOIAroundSearch:req];
//    }];
}

#pragma mark -- AMapLocationManagerDelegate

- (void)amapLocationManager:(AMapLocationManager *)manager didUpdateLocation:(CLLocation *)location reGeocode:(AMapLocationReGeocode *)reGeocode
{
    self.currentLocation = location;
    NSLog(@"location:{lat:%f; lon:%f; accuracy:%f}", location.coordinate.latitude, location.coordinate.longitude, location.horizontalAccuracy);
}

#pragma mark -- AMapSearchDelegate

- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
{
    if (response.pois.count == 0) {
        if (self.resultBlock) {
            self.resultBlock(YES, nil);
        }
        return;
    }
    if (self.resultBlock) {
        self.resultBlock(YES, response.pois);
    }
}

#pragma mark -- setters && getters

- (AMapLocationManager *)locationManager
{
    if(!_locationManager){
        _locationManager = [[AMapLocationManager alloc] init];
    }
    return _locationManager;
}

- (AMapSearchAPI *)searchManager
{
    if(!_searchManager){
        _searchManager = [[AMapSearchAPI alloc] init];
        _searchManager.delegate = self;
    }
    return _searchManager;
}
@end
